﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.Web.Components
{
    public partial class GoodsCommentsAdd : System.Web.UI.UserControl
    {
        public Guid GoodsPriceID
        {
            get
            {
                string temp = hfGoodsPriceID.Value;
                Guid guid = Guid.Empty;
                if (Guid.TryParse(temp, out guid) == false)
                {
                    guid = Guid.Empty;
                }
                return guid;
            }
            set
            {
                hfGoodsPriceID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hfGoodsPriceID.Value = GoodsPriceID.ToString();

                Things.Model.Buyer buyer = GetCurrentBuyer();
                Things.BLL.GoodsOrders goBLL = new BLL.GoodsOrders();
                Things.BLL.GoodsComments gcBLL = new BLL.GoodsComments();
                
                // 当前用户已注册，购买过商品，订单状态==已签收，并且没评论过的用户可以进行评论
                if (buyer != null
                    && goBLL.Exists(buyer.Account, GoodsPriceID) == true
                    && gcBLL.Exists(GoodsPriceID, buyer.Account) == false)
                {
                    lbtSubmit.Enabled = true;
                }
                else
                {
                    lbtSubmit.Enabled = false;
                }

                //lbtSubmit.Enabled = true;
                
            }
        }

        protected void lbtSubmit_Click(object sender, ImageClickEventArgs e)
        {
            Things.Model.Buyer buyer = GetCurrentBuyer();

            if (buyer == null)
            {
                Session[ConstantString.SessionSourceURL] = HttpContext.Current.Request.Url.PathAndQuery;
                Response.Redirect("../Login.aspx");
            }

            
            string strError = string.Empty;
            if (string.IsNullOrEmpty(this.textfield2.Value) == true)
            {
                strError += "评论标题不能为空!";
            }
             if (string.IsNullOrEmpty(this.txtComments.Text) == true)
            {
                strError += "评论内容不能为空!";
            }
 
            if (strError.Length > 0)
            {
                Things.Common.MessageBox.Show(this.Page, strError);
                return;
            }

            if ((Session[ConstantString.SessionCheckCode] != null) && (Session[ConstantString.SessionCheckCode].ToString() != ""))
            {
                if (Session[ConstantString.SessionCheckCode].ToString().ToLower() != this.CheckCode.Value.ToLower())
                {
                    this.lblMsg.Text = "所填写的验证码与所给的不符 !";
                    Session[ConstantString.SessionCheckCode] = null;
                    return;
                }
                else
                {
                    Session[ConstantString.SessionCheckCode] = null;
                }
            }

            decimal rate = 0;
            decimal.TryParse(this.rblRate.SelectedValue, out rate);
            string comments = this.txtComments.Text.Trim();

            if (buyer != null)
            {
                string userAccount = buyer.Account;
                Things.BLL.GoodsComments gcBLL = new BLL.GoodsComments();
                Things.Model.GoodsComments gcModel = new Model.GoodsComments();
                gcModel.BuyerAccount = userAccount;
                gcModel.CommentDate = DateTime.Now;
                gcModel.CommentTitle = textfield2.Value.Trim();
                gcModel.Comments = comments;
                gcModel.GoodsPriceID = GoodsPriceID;
                gcModel.Rate = rate;
                gcBLL.Add(gcModel);
            }

            
        }

        protected Things.Model.Buyer GetCurrentBuyer()
        {
            Things.Model.Buyer buyer = null;
            if (Session[ConstantString.SessionCurrentBuyerAccount] == null)
            {
                buyer = null;
            }
            else
            {
                buyer = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            }
            return buyer;
        }

    }
}